﻿using CodeGenerator.DBInfo.entity;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace CodeGenerator.DBInfo
{
    internal class MySQLDBInfoGetter : AbstractDbInfoGetter<MySqlConnection, MySqlDataAdapter>,IDBInfoGetter
    {
        public MySQLDBInfoGetter(ConnectionInfo connInfo) : base(connInfo)
        {
        }

        public override MySqlConnection Connect()
        {
            MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();
            builder.UserID = ConnInfo.UserName;
            builder.Password = ConnInfo.Password;
            builder.Server = ConnInfo.Host;
            builder.Port = ConnInfo.Port;
            return new MySqlConnection(builder.ConnectionString);
        }

        public override MySqlDataAdapter GetDataAdapter(string sql, IDbConnection conn)
        {
            return new MySqlDataAdapter(sql, conn as MySqlConnection);
        }


        public override List<string> GetDbList()
        {
            List<string> list = new List<string>();
            var dt = Query("show databases");
            foreach (DataRow row in dt.Rows)
            {
                list.Add(row["Database"].ToString());
            }
            return list;
        }

        public override List<string> GetSchemaList(string dbName)
        {
            throw new NotImplementedException();
        }

        public override TableInfo GetTableInfo(string db, string schema, string table)
        {
            string tableComment = null;
            var dt = Query($@"select
			                    t.TABLE_SCHEMA OWNER,
			                    UPPER(t.TABLE_NAME) NAME,
			                    CURRENT_USER CREATED,
			                    t.TABLE_COMMENT COMMENTS
		                     from
			                    information_schema. TABLES t
		                     where
			                    t.TABLE_SCHEMA = '{db}'");

            foreach (DataRow row in dt.Rows)
            {
                if (row["NAME"].ToString() == table)
                {
                    tableComment = row["COMMENTS"].ToString();
                }
            }
            foreach (DataRow row in dt.Rows)
            {
                if (row["NAME"].ToString() == table)
                {
                    tableComment = row["COMMENTS"].ToString();
                }
            }
            if (tableComment == null)
            {
                throw new Exception($"表 {table} 不存在！");
            }
            dt = Query($@"select
			                a.COLUMN_NAME,
			                a.DATA_TYPE,
			                a.CHARACTER_MAXIMUM_LENGTH DATA_LENGTH,
			                a.NUMERIC_PRECISION DATA_PRECISION,
			                a.NUMERIC_SCALE DATA_SCALE,
			                a.IS_NULLABLE NULLABLE,
			                a.COLUMN_COMMENT COMMENTS,
			                a.COLUMN_DEFAULT DATA_DEFAULT,
			                a.ORDINAL_POSITION COLUMN_ID,
			                a.COLUMN_KEY
		                from
				                information_schema.`COLUMNS` A
		                where
			                A.TABLE_SCHEMA = '{db}'
			                and
				                A.TABLE_NAME = '{table}'");
            var columns = new List<ColumnInfo>();
            foreach (DataRow row in dt.Rows)
            {
                columns.Add(new ColumnInfo()
                {
                    Name = row["COLUMN_NAME"].ToString(),
                    Type = row["DATA_TYPE"].ToString(),
                    IsPrimaryKey = row["IsPrimaryKey"].ToString() == "1",
                    IsAllowNull = row["NULLABLE"].ToString() == "1",
                    Comment = row["COMMENTS"].ToString(),
                });
            }
            return new TableInfo() { Name = table, Comment = tableComment, Columns = columns };
        }

        public override List<string> GetTableList(string parentName)
        {
            var list = new List<string>();
            var dt = Query($@"select
			                    t.TABLE_SCHEMA OWNER,
			                    UPPER(t.TABLE_NAME) NAME,
			                    CURRENT_USER CREATED,
			                    t.TABLE_COMMENT COMMENTS
		                     from
			                    information_schema. TABLES t
		                     where
			                    t.TABLE_SCHEMA = '{parentName}'");

            foreach (DataRow row in dt.Rows)
            {
                list.Add(row["NAME"].ToString());
            }
            return list;
        }

        public override void TestConnect()
        {
            Connect().Open();
        }
    }
}
